var urlParamObj = $$.util.urlParamToObj();
var pageSize = 20;
var userType = urlParam.usertype;
if(urlParamObj.phone && urlParamObj.token && urlParamObj.ctype && urlParamObj.usertype){
    userType = urlParamObj.usertype;
}
var getFollowupSteps = function (currentPage) {//页面更新时
    //渲染总页码
    var totalPage = Math.ceil(urlParamObj.total / pageSize);
    if(totalPage == 1){
        $('.page-separate').remove();
    }else{
        $('.all-page').text(totalPage);
    }
    currentPage = currentPage || urlParamObj.num;
    $('#currentPage').val(currentPage);
    $('#pagerBox .arrow').removeClass('disabled');
    if(currentPage == 1){
        $('#pagerBox .previous').addClass('disabled');
    }else if(currentPage == totalPage){
        $('#pagerBox .next').addClass('disabled');
    }
    new followupPlan().getFollowupSteps(urlParamObj.planid,currentPage,pageSize,userType,function (res, status) {
        res = JSON.parse(res);
        if(res.code == '000'){
            $('#b1').html('');
            var resData=JSON.parse(res.data);//返回的data数据
            var len=resData.length;
            for(var i=0;i<len;i++){
                var nodeTime=new Date(resData[i].enddatetime);//时间戳转化
                var timeAll=$$.util.timeStampToDay(nodeTime);
                new pageRendering(i + 1 + (+pageSize * (+currentPage - 1)),resData[i].templatestep.periodtype,resData[i].title,resData[i].description,timeAll,(resData[i].form && resData[i].form.inputformdata || []),resData[i]);
            }
            $('.outer').addClass('active');
            $('#b1').attr('data-page',currentPage);
            $('html,body').animate({scrollTop: 0}, 200);
        }else{
            $$.modal.tips('加载失败',1500);
        }
    });
};
//页码相关
var pager = function () {
    var totalPage = Number($('.all-page').text());
    //前后页切换
    $('#pagerBox .arrow').click(function () {
        if($(this).hasClass('disabled')) return false;
        var currentPage = $('#b1').attr('data-page');
        if($(this).hasClass('previous')){
            getFollowupSteps(--currentPage);
        }else{
            getFollowupSteps(++currentPage);
        }
    });
    //页码跳转
    $('#currentPage').keyup(function (e) {
        if(e.keyCode == 13){
            var num = Number($(this).val());
            (num < 1) && (num = 1);
            (num > totalPage) && (num = totalPage);
            getFollowupSteps(num);
        }
    });
};
// 页面渲染封装
var pageRendering=function(index,type,title,description,time,forms,resData){
    var desHtml = ' ';
    if(description && description.trim()){
        desHtml = "<a href='javascript:;' class='btn step-description' >"+description+"</a>";
    }
    var html=document.createElement('div');
    this.html=html;
    var $html=$(html);
    var formUrl = './form.html?';
    if(urlParamObj.phone && urlParamObj.token && urlParamObj.ctype && urlParamObj.usertype){
        formUrl = './form.html?phone='+urlParamObj.phone + '&token='+urlParamObj.token + '&ctype='+urlParamObj.ctype + '&usertype='+urlParamObj.usertype + '&';
    }
    html.setAttribute('class','node-item-box');
    var editTimeHtml ='';
    if(type == 0){
        index = '<i class="left-index material-icons">&#xE873;</i>';
        editTimeHtml = '<i class="material-icons edit-time">&#xE254;</i>';
    }
    html.innerHTML="<div class='row'>"+
    "<div class='col-md-1 col-xs-1'>"+
    "<span  class='biaoJ'/>"+ index +"</span>"+
    "</div>"+
    "<div class='col-md-3 col-xs-10 node-top'>"+
    "<span class='xiaoBt'><a href='"+ formUrl +"stepid="+resData.id+"&status="+ urlParamObj.status +"'>"+title+"</a></span>"+
    "</div>"+
    "<div class='col-md-8  col-xs-12 text-right node-top'>"+
    "<div class='fu-start-time'><span class='xiaoBt'>"+time+"</span>"+ editTimeHtml +"</div>"+
    "</div> </div>"+
    "<div class='row'>"+
    "<div class='col-md-11 col-xs-11 col-md-offset-1 col-xs-offset-1 description-text'>"+
    desHtml +
    "</div> </div>";
    var len = forms.length;
    if(len){
        forms = forms.filter(function (e){
            //过滤仅医生查看量表
            if(e.owner != 3 || userType != 2){
                return e;
            }
        });
        len = forms.length;
        for(var i = 0;i<len;i++){
            var abtn=document.createElement('a');
            if(forms[i].owner==2){
                abtn.setAttribute('class','btn jbbtn green');
            }else if(forms[i].owner==1||forms[i].owner==3){
                abtn.setAttribute('class','btn jbbtn blue');
            }
            abtn.setAttribute('href',formUrl + 'stepid='+resData.id+'&i='+i + '&status='+ urlParamObj.status);
            abtn.innerHTML=(i+1)+'、'+forms[i].title;
            $html.find('.description-text').append(abtn);
        }
    }
    $('#b1').append(html);
};
//取消、完成随访计划
var cancleOrComplete=function(){
  $('#finishPlanBtn').click(function(){
      new followupPlan().finishOrCancel(urlParamObj.planid,'2',function(res,status){
          res = JSON.parse(res);
          if(res.code == "000"){
              location.href='./datalist.html';
          }
      })
  });
  $('#cancelPlanBtn').click(function(){
      new followupPlan().finishOrCancel(urlParamObj.planid,'3',function(res,status){
          res=JSON.parse(res);
          if(res.code == '000'){
              location.href='./datalist.html';
          }
      })
  })
};
//编辑参考节点时间
var editTime = function () {
    $('body').on('click','.fu-start-time .edit-time',function () {
        var text;
        if($(this).attr('data-edit') == 'true'){
            $(this).removeAttr('data-edit');
            $(this).html('&#xE254;');
            text = $(this).siblings('.xiaoBt').find('.input-date-box').val();
            $(this).siblings('.xiaoBt').html(text);
            new followupPlan().changeTime(urlParamObj.planid,text + ' 00:00:00',$(this).parents('.node-item-box').index(),function (res, status) {
                res = JSON.parse(res);
                if(res.code == '000'){
                    $$.modal.tips('参考节点时间更新成功',1500);
                    setTimeout(function () {
                        location.reload();
                    },1500);
                }
            })
        }else{
            $(this).attr('data-edit','true');
            $(this).html('&#xE5CA;');
            text = $(this).siblings('.xiaoBt').text();
            $(this).siblings('.xiaoBt').html('<input class="input-date-box" type="text" value="'+ text +'"/>');
            timepicker();
            $(this).siblings('.xiaoBt').find('.input-date-box').datetimepicker('show');
        }
    });
};
//装载时间插件
var timepicker = function(){
    $('.input-date-box').datetimepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
        todayBtn: true,
        minView:2,
        pickerPosition:"bottom-left"
    });
};
$(function(){
    $('#moreVert').click(function(){
        $('#motai').show();
        $('#timeMotai').show();
    });
    $('#motai').click(function(){
        $('#motai').hide();
        $('#timeMotai').hide();
    });
    if(urlParamObj.status == 2){
        $('#moreVert').css('visibility','hidden');
    }
    /*窗口变化事件====*/
    $('#timeNav').css({'width':$('.mid-box').width()});
    $(window).resize(function(){
        $('#timeNav').css({'width':$('.mid-box').width()});
    });
    getFollowupSteps();
    cancleOrComplete();
    editTime();
    pager();
});
